﻿using PosClient.Controls.ViewModel;
using System.Drawing;
using System.Windows.Forms;
using ReactiveUI;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System;
using PosClient.Delegates;

namespace PosClient.Controls
{
    public partial class PluRow : UserControl , IViewFor<PluRowViewModel>
    {
        object IViewFor.ViewModel
        {
            get => ViewModel;
            set => ViewModel = (PluRowViewModel)value;
        }
        public PluRowViewModel ViewModel { get; set; }

        public event RowClickEventHandler RowClick;

        public event RowValueEventHandler RowValue;

        public PluRow()
        {
            InitializeComponent();

            InitBinding();

            InitEvent();
        }

        private void InitBinding()
        {
            ViewModel = new PluRowViewModel();
            this.WhenActivated(d =>
            {
                this.OneWayBind(ViewModel,
                    vm => vm.Plu.PluSvg,
                    v => v.avatar_plu.ImageSvg
                    ).DisposeWith(d);

                this.OneWayBind(ViewModel,
                    vm => vm.Plu.PluImage,
                    v => v.avatar_plu.Image
                    ).DisposeWith(d);

                this.OneWayBind(ViewModel,
                    vm => vm.Plu.PluName,
                    v => v.label_pluname.Text
                    ).DisposeWith(d);

                this.OneWayBind(ViewModel,
                    vm => vm.Plu.Amount,
                    v => v.label_amount.Text
                    ).DisposeWith(d);

                this.OneWayBind(ViewModel,
                    vm => vm.Plu.Amount,
                    v => v.label_amount.Text
                    ).DisposeWith(d);

                this.OneWayBind(ViewModel,
                    vm => vm.Plu.Count,
                    v => v.label_count.Text
                    ).DisposeWith(d);

                this.Bind(ViewModel,
                    vm => vm.IsChecked,
                    v => v.panel_back.Back,
                    ConvertColor,
                    ConvertBool
                    ).DisposeWith(d);

                this.OneWayBind(ViewModel,
                    vm => vm.Screen,
                    v => v.label_close.Visible
                    ).DisposeWith(d);

                this.OneWayBind(ViewModel,
                    vm => vm.Screen,
                    v => v.label_del.Visible
                    ).DisposeWith(d);

                this.OneWayBind(ViewModel,
                    vm => vm.Screen,
                    v => v.label_add.Visible
                    ).DisposeWith(d);

                this.OneWayBind(ViewModel,
                    vm => vm.Screen,
                    v => v.label_change.Visible
                    ).DisposeWith(d);

                this.label_add.Events().Click.Do(x =>
                {
                    RowValue?.Invoke("+", null);
                    RowClick?.Invoke(this, x);
                })
                .Subscribe()
                .DisposeWith(d);

                this.label_del.Events().Click.Do(x =>
                {
                    RowValue?.Invoke("-", null);
                    RowClick?.Invoke(this, x);
                })
                .Subscribe()
                .DisposeWith(d);

                ViewModel.WhenAnyValue(x => x.Plu.Count).Do(y =>
                {
                    if (y < 0)
                    {
                        Dispose();
                    }
                })
                .Subscribe()
                .DisposeWith(d);

            });
        }

        private bool ConvertBool(Color? arg)
        {
            return arg == Color.FromArgb(247, 247, 247);
        }

        private Color? ConvertColor(bool arg)
        {
            return arg ? Color.FromArgb(247, 247, 247) : Color.White ;
        }

        private void InitEvent()
        {
            this.Click += (s, e) =>
            {
                RowClick?.Invoke(s, e);
            };

            this.gridPanel1.Click += (s, e) =>
            {
                RowClick?.Invoke(this, e);
            };

            this.gridPanel2.Click += (s, e) =>
            {
                RowClick?.Invoke(this, e);
            };

            this.label_amount.Click += (s, e) =>
            {
                RowClick?.Invoke(this, e);
            };

            this.avatar_plu.Click += (s, e) =>
            {
                RowClick?.Invoke(this, e);
            };

            this.panel_back.Click += (s, e) =>
            {
                RowClick?.Invoke(this, e);
            };

            this.label_count.Click += (s, e) =>
            {
                RowClick?.Invoke(this, e);
            };

            this.label_change.Click += (s, e) =>
            {
                RowClick?.Invoke(this, e);
            };

            this.label_pluname.Click += (s, e) =>
            {
                RowClick?.Invoke(this, e);
            };

            this.stackPanel1.Click += (s, e) =>
            {
                RowClick?.Invoke(this, e);
            };

            this.label_close.Click += (s, e) =>
            {
                this.Dispose();
            };
        }
    }
}
